package ljt.music.utils;

import android.app.ActivityManager;
import android.content.Context;
import android.support.annotation.NonNull;

import static android.content.Context.ACTIVITY_SERVICE;

/**
 * Created by jiangtao on 2018/1/3.
 */

public class APPUtils {
  /**
   * 判断服务是否还在运行
   *
   * @param context     上下文
   * @param serviceName 服务名(Reference) 例如：com.imyuu.travel.service.media.MusicService
   * @return 是否在运行
   */
  public static boolean isServiceRunning(@NonNull Context context, @NonNull String serviceName) {
    ActivityManager manager = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
      if (serviceName.equals(service.service.getClassName())) {
        return true;
      }
    }
    return false;
  }
}
